package com.myapp.transition;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.cdi.BusinessProcess;
import org.camunda.bpm.engine.cdi.compat.CamundaTaskForm;
import org.camunda.bpm.engine.cdi.jsf.TaskForm;

@Named
@ConversationScoped
public class ScreenAController implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@Inject
	private BusinessProcess businessProcess;
	
	@Inject
	private TaskService taskService;
	
	@Inject
	private RuntimeService runtimeService;
	
	@Inject
	private TaskForm taskForm;
	
	@Inject
	private CamundaTaskForm camundaTaskForm;
	
	@Inject
	private MyTaskForm myTaskForm;

	private String getNewTaskId() {
		String processInstanceId = businessProcess.getProcessInstanceId();
		System.out.println("processInstanceId: " + processInstanceId);
		
		return taskService.createTaskQuery().processInstanceId(businessProcess.getProcessInstanceId()).list().get(0).getId();
	}
	
	public void save() throws IOException {
		log();
		
		String newTaskId = getNewTaskId();
		System.out.println("newTaskId: " + newTaskId);
		businessProcess.startTask(newTaskId);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("next", true);
		taskService.complete(businessProcess.getTaskId(), map);
	}
	
	private void log() {
		String taskId = businessProcess.getTaskId();
		System.out.println("taskId: " + taskId);
		
		String executionId = businessProcess.getExecutionId();
		System.out.println("executionId: " + executionId);
	}
}
